<?php

class AlbumsController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('image', 'update', 'deleteAlbum', 'mainPhoto', 'deletePhoto', 'comments', 'create', 'index','view', 'upload', 'uploadPhotoPopup','uploadPhotoPopupGroup','uploadPhotoPopupEvent', 'uploadPhoto', 'photo', 'insertLike', 'deleteLike', 'insertComment', 'deleteComment','deleteGroup','deleteEvent','createGroup','createEvent','indexGroup','indexEvent','viewGroup','viewEvent'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array(),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	public function actionDeleteAlbum($id)
	{
		$this->loadModel($id)->delete();
	}
	public function actionDeletePhoto($id)
	{
		$model = Photos::model()->findByPk($id);
		$model->delete();

		if(isset( $model->album->user_id )) {
			$path = Yii::app()->createUrl('albums/upload', array('id'=>$model->album->id, 'profile'=>Yii::app()->request->getParam('profile')));
		} elseif (isset( $model->album->group_id )) {
			$path = Yii::app()->createUrl('albums/uploadGroup', array('id'=>$model->album->id, 'group'=>Yii::app()->request->getParam('group')));
		} else {
			$path = Yii::app()->createUrl('albums/uploadEvent', array('id'=>$model->album->id, 'event'=>Yii::app()->request->getParam('event')));
		}
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : $path);
	
	}
	public function actionMainPhoto($id)
	{
		$model = Photos::model()->findByPk($id);
		$album=$this->loadModel($model->album->id);
		$album->main_photo_id = $id;
		$album->save();

		if(isset( $model->album->user_id )) {
			$path = Yii::app()->createUrl('albums/upload', array('id'=>$model->album->id, 'profile'=>Yii::app()->request->getParam('profile')));
		} elseif (isset( $model->album->group_id )) {
			$path = Yii::app()->createUrl('albums/uploadGroup', array('id'=>$model->album->id, 'group'=>Yii::app()->request->getParam('group')));
		} else {
			$path = Yii::app()->createUrl('albums/uploadEvent', array('id'=>$model->album->id, 'event'=>Yii::app()->request->getParam('event')));
		}
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : $path);
	}
	public function actionUpdate($id)
	{
		if(Yii::app()->request->getParam('profile') == Yii::app()->user->id)
		{
			$model=$this->loadModel($id);
			if(isset($_POST['Albums']))
			{
				$model->attributes=$_POST['Albums'];
				if($model->save())
					$this->actionIndex();
			} else {
				$this->render('update',array(
						'model'=>$model,
				));
			}
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	// add comment
	public function actionInsertComment()
	{
		if (Yii::app()->request->isAjaxRequest) {
			$data = array();
	
			if(isset($_POST['text'])){
				$data['text'] = $_POST['text'];
			}else{
				$data['text'] = '';
			}
	
			if(isset($_GET['id'])){
				$data['id'] = $_GET['id'];
			}else{
				$data['id'] = 0;
			}
	
			if(isset($_GET['parent_id'])){
				$data['parent_id'] = $_GET['parent_id'];
			}else{
				$data['parent_id'] = 0;
			}
	
			if($data['id']){
				$make = new PhotosComments;
				$make->text = $data['text'];
				$make->photo_id = $data['id'];
				$make->user_id = Yii::app()->user->id;
				$make->parent_id = $data['parent_id'];
				$make->crdate = date("Y-m-d H:i:s");
	
				$make->save();
	
				$data['comment_id'] =  $make->getPrimaryKey();
				$user_info = Users::model()->findByPk(Yii::app()->user->id);
	
	
				$data['user'] = array(
					'name' => $user_info->full_name,
					'id' => $user_info->id,
					'avatar' => $user_info->avatar
				);

				$email_to = Photos::model()->findByPk( $make->photo_id )->user;
				if( ($email_to->comment_notification == 1) && ($email_to->last_visit < date("Y-m-d H:i:s", time() - 15*60)) ) 
				{
					Yii::app()->getModule('email');
					$email = new Email;
					$email->from = $user_info->email;
					$email->layout = 'common';
					$email->type = 'text/html';
					$email->to = $email_to->email;
					$email->view = 'main';
					$email->send(array('header' => 'Новый коментарий',
						'content' => $email_to->full_name . ', вы получили новое сообещение от '
							. $user_info->full_name . '<br>'
							. $data['text'] . '<br>'
							. 'Получено ' . Yii::app()->dateFormatter->formatDateTime(date("Y-m-d H:i:s"), 'long','short')
					));
					// print_r($email);
				}
	
				$this->renderPartial('_newComment',array(
					'comment'=>$data
				));
			}
		}else{
			$this->redirect(Yii::app()->createUrl('albums/index'));
		}
	}
	// delete comment
	public function actionDeleteComment(){
		if (Yii::app()->request->isAjaxRequest) {
			if(isset($_GET['comment_id'])){
				PhotosComments::model()->deleteAll("id = '" . $_GET['comment_id'] . "'");
			}
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}
	public function actionCreate()
	{
		$model=new Albums;
		if(isset($_POST['Albums']))
		{
			$data = $_POST['Albums'];
			$data['user_id'] = Yii::app()->user->id;
			$model->attributes=$data;
			if($model->save()){
				$id =  $model->getPrimaryKey();
				$this->actionIndex();
			} else {
				$this->render('create',array(
						'model'=>$model,
				));
			}
		} else {
			$this->render('create',array(
					'model'=>$model,
			));
		}
	}
	public function actionIndex()
	{
		$user_id = isset($_GET['profile']) ? $_GET['profile']: Yii::app()->user->id;

		$user_info = Users::model()->findByPk($user_id, array('select'=>'view_photo'));
		if($user_id == Yii::app()->user->id || $user_info->view_photo == 1 || ($user_info->view_photo==2 && $user_info->checkFriend) )
		{
			$dataProvider = Albums::model()->findAll(
				'user_id=:user_id',
				array(':user_id'=>$user_id)
			);
			$photosCount = 0;
			$photos = array();
			foreach ($dataProvider as $key => $value){
				$photosCount = $photosCount+count($value->photos);
				$photos = array_merge($photos, $value->photos);
			}
			$this->render('index',array(
				'model'=>$dataProvider,
				'albums'=>count($dataProvider),
				'photos'=>$photosCount,
				'lastphotos'=>$photos,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	public function actionComments()
	{
		$user_id = isset($_GET['profile']) ? $_GET['profile']: Yii::app()->user->id;
		$dataProvider = Albums::model()->findAll(
				'user_id=:user_id',
				array(':user_id'=>$user_id)
		);
		$photosCount = 0;
		$photos = array();
		$comments = array();
		foreach ($dataProvider as $key => $value){
			$photosCount = $photosCount+count($value->photos);
			$photos = array_merge($photos, $value->photos);
		}
		foreach ($photos as $key => $value){
			$comments = isset($value->comments) ? array_merge($comments, $value->comments) : $comments;
		}
		$this->render('comments',array(
			'comments'=>$comments,
			'albums'=>count($dataProvider),
			'photos'=>$photosCount,
		));
	}
	public function actionView($id)
	{
		$user_id = isset($_GET['profile']) ? $_GET['profile']: Yii::app()->user->id;
		$user_info = Users::model()->findByPk($user_id, array('select'=>'view_photo'));
		if($user_id == Yii::app()->user->id || $user_info->view_photo == 1 || ($user_info->view_photo==2 && $user_info->checkFriend) )
		{
			Yii::app()->session['albums'] = $id;
			$dataProvider = Albums::model()->findAll(
				'user_id=:user_id',
				array(':user_id'=>$user_id)
			);
			$photos = 0;
			foreach ($dataProvider as $key => $value){
				$photos = $photos+count($value->photos);
			}
			$this->render('view',array(
				'model'=>$this->loadModel($id),
				'albums'=>count($dataProvider),
				'photos'=>$photos,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	public function actionUpload($id)
	{
		$user_id = isset($_GET['profile']) ? $_GET['profile']: Yii::app()->user->id;
		if($user_id == Yii::app()->user->id)
		{
			Yii::app()->session['albums'] = $id;
			Yii::app()->session['user'] = $user_id;
			Yii::app()->session['path'] = 'photos';
			$dataProvider = Albums::model()->findAll(
					'user_id=:user_id',
					array(':user_id'=>$user_id)
			);
			$photos = 0;
			foreach ($dataProvider as $key => $value){
				$photos = $photos+count($value->photos);
			}
			$this->render('upload',array(
					'model'=>$this->loadModel($id),
					'albums'=>count($dataProvider),
					'photos'=>$photos,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	public function actionUploadPhotoPopup(){
		$model=new Photos;
		$model->attributes = array('image'=>'');
		$model->album_id = '0';
		$model->user_id = Yii::app()->user->id;
		$model->crdate = date("Y-m-d H:i:s");
		$path = $_SERVER['DOCUMENT_ROOT'].'/upload/photos/'.Yii::app()->user->id.'/0/';
		$model->name = self::savePhoto($_FILES['image'], $path);
		$model->save();
		echo "{";
		echo				"msg: '" . $model->id . "',\n";
		//echo				"img: '" . $html . "'\n";
		echo				"img: '" . $model->name . "',\n";
		echo				"path: '" . '/upload/photos/'. Yii::app()->user->id. '/' . $model->album_id . "/'\n";
		echo "}";
	}
	public function actionUploadPhoto(){
		$model=new Photos;
		
		if(Yii::app()->request->getParam('id')!=NULL)
		{
			$model->attributes = array('image'=>'');
			$model->album_id = Yii::app()->request->getParam('id');
			$model->user_id = Yii::app()->user->id;
			$model->crdate = date("Y-m-d H:i:s");
			$path = $_SERVER['DOCUMENT_ROOT'].'/upload/photos/'.Yii::app()->user->id.'/'.Yii::app()->request->getParam('id').'/';
			$model->name = self::savePhoto($_FILES['image'], $path); 
			$model->save();
			echo "{";
			echo				"msg: '" . $model->name . "'\n";
			echo "}";
		}
	}
	public function actionPhoto($id)
	{
		$model = Photos::model()->findByPk($id);
		$photos = $model->album->photos;
		$all = count($photos);
		$prev = $photos[$all-1];
		$next = $photos[0];
		$crnt = '';
		foreach ($photos as $key => $value){
			$prev = $id==$value->id && isset($photos[$key-1]) ? $photos[$key-1] : $prev;
			$next = $id==$value->id && isset($photos[$key+1]) ? $photos[$key+1] : $next;
			$crnt = $id==$value->id ? $key+1 : $crnt;
		}
		$param = array(
			'data'=>$model,
			'next'=>$next,
			'prev'=>$prev,
			'pos'=>$crnt.'/'.$all
		);
		$this->renderPartial('photoAjax', $param );
	}
	/*
	public function actionImage($id)
	{
		$model = Users::model()->findByPk($id);
		$photos = $model->avatar;
		$param = array(
				'data'=>$model,
				'next'=>'',
				'prev'=>'',
				'pos'=>''
		);
		$this->renderPartial('photoAjax', $param );
	}*/
	// comment like add
	public function actionInsertLike(){
		if (Yii::app()->request->isAjaxRequest) {
			if(isset($_GET['photo_id'])){
				$make = new PhotosLikes;
				$make->photo_id = $_GET['photo_id'];
				$make->user_id = Yii::app()->user->id;
				$make->save();
	
				$photo_like_id = $make->getPrimaryKey();
					
				$data['href_dislike'] = Yii::app()->createUrl('albums/deletelike', array('photo_id'=>$_GET['photo_id'], 'photo_like_id'=>$photo_like_id));
					
				echo CJSON::encode($data);
			}
		}else{
			$this->redirect(Yii::app()->createUrl('albums/index'));
		}
	}
	
	// comment like delete
	public function actionDeleteLike(){
		if (Yii::app()->request->isAjaxRequest) {
			if(isset($_GET['photo_like_id'])){
				PhotosLikes::model()->deleteAll("id = '" . $_GET['photo_like_id'] . "'");
	
				$data['href_like'] = Yii::app()->createUrl('albums/insertlike', array('photo_id'=>$_GET['photo_id']));
	
				echo CJSON::encode($data);
			}
		}else{
			$this->redirect(Yii::app()->createUrl('albums/index'));
		}
	}
	public function actionUpdateGroup($id)
	{
		$model=$this->loadModel($id);

		if($model->group->checkAdmin )
		{
			if(isset($_POST['Albums']))
			{
				$model->attributes=$_POST['Albums'];
				if($model->save())
					$this->redirect(Yii::app()->createUrl('albums/viewGroup', array('id'=>$model->id, 'group'=>Yii::app()->request->getParam('group'))));
			}
		
			$this->render('updateGroup',array(
					'model'=>$model,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}

	}
	public function actionUpdateEvent($id)
	{
		$model=$this->loadModel($id);
		if($model->event->group->checkAdmin)
		{
			if(isset($_POST['Albums']))
			{
				$model->attributes=$_POST['Albums'];
				if($model->save())
					$this->redirect(Yii::app()->createUrl('albums/viewEvent', array('id'=>$model->id, 'event'=>Yii::app()->request->getParam('event'))));
			}
		
			$this->render('updateEvent',array(
					'model'=>$model,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	
	
	public function actionCreateGroup($group)
	{
		$model=new Albums;
		if(isset($_POST['Albums']))
		{
			$_POST['Albums']['group_id'] = $group;
			$model->attributes=$_POST['Albums'];
			/*if($model->save()){
				$id =  $model->getPrimaryKey();
				$this->redirect(Yii::app()->createUrl('albums/viewGroup', array('id'=>$id, 'group'=>Yii::app()->request->getParam('group'))));
			}*/
			if($model->save()){
				$id =  $model->getPrimaryKey();
				$this->actionIndexGroup($group);
			} else {
				$this->render('createGroup',array(
						'model'=>$model,
				));
			}
		}
		$this->render('createGroup',array(
				'model'=>$model,
		));
	}
	public function actionCreateEvent($event)
	{
		$model=new Albums;
		if(isset($_POST['Albums']))
		{
			$_POST['Albums']['event_id'] = $event;
			$model->attributes=$_POST['Albums'];
			/*if($model->save()){
				$id =  $model->getPrimaryKey();
				$this->redirect(Yii::app()->createUrl('albums/viewEvent', array('id'=>$id, 'event'=>Yii::app()->request->getParam('event'))));
			}*/
			if($model->save()){
				$id =  $model->getPrimaryKey();
				$this->actionIndexEvent($event);
			} else {
				$this->render('createEvent',array(
					'model'=>$model,
				));
			}
		}
		$this->render('createEvent',array(
				'model'=>$model,
		));
	}

	
	public function actionIndexGroup($group)
	{
		$model = Groups::model()->findByPk($group);
		$checkOwner = $model->checkAdmin;
		if ($model->approve == 1 || ( isset($model->checkUser) AND $model->checkUser->status == 1 ))
		{
			$dataProvider = Albums::model()->findAll(
				'group_id=:group_id',
				array(':group_id'=>$group)
			);
			$photosCount = 0;
			$photos = array();
			foreach ($dataProvider as $key => $value){
				$photosCount = $photosCount+count($value->photos);
				$photos = array_merge($photos, $value->photos);
			}
			$this->render('indexGroup',array(
				'model'		=>	$dataProvider,
				'albums'	=>	count($dataProvider),
				'photos'	=>	$photosCount,
				'lastphotos'=>	$photos,
				'checkOwner'=>	$checkOwner,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	public function actionIndexEvent($event)
	{
		$model = Events::model()->findByPk($event)->group;
		$checkOwner = $model->checkAdmin;
		if ($model->approve == 1 || ( isset($model->checkUser) AND $model->checkUser->status == 1 ))
		{

			$dataProvider = Albums::model()->findAll(
				'event_id=:event_id',
				array(':event_id'=>$event)
			);
			$photosCount = 0;
			$photos = array();
			foreach ($dataProvider as $key => $value){
				$photosCount = $photosCount+count($value->photos);
				$photos = array_merge($photos, $value->photos);
			}
			$this->render('indexEvent',array(
				'model'		=>	$dataProvider,
				'albums'	=>	count($dataProvider),
				'photos'	=>	$photosCount,
				'lastphotos'=>	$photos,
				'checkOwner'=>	$checkOwner,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	
	// ViewGroup
	public function actionViewGroup($id,$group)
	{
		$model = Groups::model()->findByPk($group);
		$checkOwner = $model->checkAdmin;
		if ($model->approve == 1 || ( isset($model->checkUser) AND $model->checkUser->status == 1 ))
		{
			$dataProvider = Albums::model()->findAll(
				'group_id=:group_id',
				array(':group_id'=>$group)
			);
			$photos = 0;
			foreach ($dataProvider as $key => $value){
				$photos = $photos+count($value->photos);
			}
			$this->render('viewGroup',array(
				'model'=>$this->loadModel($id),
				'albums'=>count($dataProvider),
				'photos'=>$photos,
				'checkOwner'=>$checkOwner,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	// ViewEvent
	public function actionViewEvent($id,$event)
	{
		$model = Events::model()->findByPk($event)->group;
		$checkOwner = $model->checkAdmin;
		if ($model->approve == 1 || ( isset($model->checkUser) AND $model->checkUser->status == 1 ))
		{
			$dataProvider = Albums::model()->findAll(
				'event_id=:event_id',
				array(':event_id'=>$event)
			);
			$photos = 0;
			foreach ($dataProvider as $key => $value){
				$photos = $photos+count($value->photos);
			}
			$this->render('viewEvent',array(
				'model'=>$this->loadModel($id),
				'albums'=>count($dataProvider),
				'photos'=>$photos,
				'checkOwner'=>$checkOwner,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}
	public function actionCommentsGroup($group)
	{
		$checkOwner = Groups::model()->findByPk($group)->checkAdmin;
		$dataProvider = Albums::model()->findAll(
			'group_id=:group_id',
			array(':group_id'=>$group)
		);
		$photosCount = 0;
		$photos = array();
		$comments = array();
		foreach ($dataProvider as $key => $value){
			$photosCount = $photosCount+count($value->photos);
			$photos = array_merge($photos, $value->photos);
		}
		foreach ($photos as $key => $value){
			$comments = isset($value->comments) ? array_merge($comments, $value->comments) : $comments;
		}
		$this->render('commentsGroup',array(
			'comments'=>$comments,
			'albums'=>count($dataProvider),
			'photos'=>$photosCount,
			'checkOwner'=>$checkOwner,
		));
	}
	public function actionCommentsEvent($event)
	{
		$checkOwner = Events::model()->findByPk($event)->group->checkAdmin;
		$dataProvider = Albums::model()->findAll(
			'event_id=:event_id',
			array(':event_id'=>$event)
		);
		$photosCount = 0;
		$photos = array();
		$comments = array();
		foreach ($dataProvider as $key => $value){
			$photosCount = $photosCount+count($value->photos);
			$photos = array_merge($photos, $value->photos);
		}
		foreach ($photos as $key => $value){
			$comments = isset($value->comments) ? array_merge($comments, $value->comments) : $comments;
		}
		$this->render('commentsEvent',array(
			'comments'=>$comments,
			'albums'=>count($dataProvider),
			'photos'=>$photosCount,
			'checkOwner'=>$checkOwner,
		));
	}
	public function actionDeleteGroup($id)
	{
		$this->loadModel($id)->delete();
		
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : Yii::app()->createUrl('albums/indexGroup', array('group'=>Yii::app()->request->getParam('group'))));
	}
	public function actionDeleteEvent($id)
	{
		$this->loadModel($id)->delete();
		
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : Yii::app()->createUrl('albums/indexEvent', array('event'=>Yii::app()->request->getParam('event'))));
	}

	public function actionUploadGroup($id,$group)
	{
		$model = Groups::model()->findByPk($group);
		$checkOwner = $model->checkAdmin;
		if ($model->approve == 1 || ( isset($model->checkUser) AND $model->checkUser->status == 1 ))
		{
			Yii::app()->session['user'] = $group;
			Yii::app()->session['path'] = 'groups';
			Yii::app()->session['albums'] = $id;
			$dataProvider = Albums::model()->findAll(
					'group_id=:group_id',
					array(':group_id'=>$group)
			);
			$photos = 0;
			foreach ($dataProvider as $key => $value){
				$photos = $photos+count($value->photos);
			}
			$this->render('uploadGroup',array(
					'model'=>$this->loadModel($id),
					'albums'=>count($dataProvider),
					'photos'=>$photos,
					'checkOwner'=>$checkOwner,
					'group_id'=>$group,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}

	public function actionUploadEvent($id,$event)
	{
		$model = Events::model()->findByPk($event)->group;
		$checkOwner = $model->checkAdmin;
		if ($model->approve == 1 || ( isset($model->checkUser) AND $model->checkUser->status == 1 ))
		{
			Yii::app()->session['user'] = $event;
			Yii::app()->session['path'] = 'events';
			Yii::app()->session['albums'] = $id;
			$dataProvider = Albums::model()->findAll(
					'event_id=:event_id',
					array(':event_id'=>$event)
			);
			$photos = 0;
			foreach ($dataProvider as $key => $value){
				$photos = $photos+count($value->photos);
			}
			$this->render('uploadEvent',array(
					'model'=>$this->loadModel($id),
					'albums'=>count($dataProvider),
					'photos'=>$photos,
					'checkOwner'=>$checkOwner,
					'event_id'=>$event,
			));
		}
		else
		{
			$this->redirect(Yii::app()->createUrl('profile/index',array('profile'=>Yii::app()->user->id)));
		}
	}

	public function actionUploadPhotoPopupGroup(){
		$model=new Photos;
		$model->attributes = array('image'=>'');
		$model->album_id = '0';
		$model->user_id = Yii::app()->user->id;
		$model->crdate = date("Y-m-d H:i:s");
		$path = $_SERVER['DOCUMENT_ROOT'].'/upload/groups/'. Yii::app()->request->getParam('group') .'/0/';
		$model->name = self::savePhoto($_FILES['image'], $path);
		$model->save();
		echo "{";
		echo				"msg: '" . $model->id . "',\n";
		//echo				"img: '" . $html . "'\n";
		echo				"img: '" . $model->name . "',\n";
		echo				"path: '" . '/upload/groups/'. Yii::app()->request->getParam('group') . '/' . $model->album_id . "/'\n";
		echo "}";
	}
	public function actionUploadPhotoPopupEvent(){
		$model=new Photos;
		$model->attributes = array('image'=>'');
		$model->album_id = '0';
		$model->user_id = Yii::app()->user->id;
		$model->crdate = date("Y-m-d H:i:s");
		$path = $_SERVER['DOCUMENT_ROOT'].'/upload/events/'. Yii::app()->request->getParam('event') .'/0/';
		$model->name = self::savePhoto($_FILES['image'], $path);
		$model->save();
		echo "{";
		echo				"msg: '" . $model->id . "',\n";
		//echo				"img: '" . $html . "'\n";
		echo				"img: '" . $model->name . "',\n";
		echo				"path: '" . '/upload/events/'. Yii::app()->request->getParam('event') . '/' . $model->album_id . "/'\n";
		echo "}";
	}
	public function actionUploadPhotoGroup(){
		$model=new Photos;
		
		if(Yii::app()->request->getParam('id')!=NULL)
		{
			$model->attributes = array('image'=>'');
			$model->album_id = Yii::app()->request->getParam('id');
			$model->user_id = Yii::app()->user->id;
			$model->crdate = date("Y-m-d H:i:s");
			$path = $_SERVER['DOCUMENT_ROOT'].'/upload/groups/'.Yii::app()->request->getParam('group').'/'.Yii::app()->request->getParam('id').'/';
			$model->name = self::savePhoto($_FILES['image'], $path); 
			$model->save();
			echo "{";
			echo				"msg: '" . $model->name . "'\n";
			echo "}";
		}
	}
	public function actionUploadPhotoEvent(){
		$model=new Photos;
		
		if(Yii::app()->request->getParam('id')!=NULL)
		{
			$model->attributes = array('image'=>'');
			$model->album_id = Yii::app()->request->getParam('id');
			$model->user_id = Yii::app()->user->id;
			$model->crdate = date("Y-m-d H:i:s");
			$path = $_SERVER['DOCUMENT_ROOT'].'/upload/events/'.Yii::app()->request->getParam('event').'/'.Yii::app()->request->getParam('id').'/';
			$model->name = self::savePhoto($_FILES['image'], $path); 
			$model->save();
			echo "{";
			echo				"msg: '" . $model->name . "'\n";
			echo "}";
		}
	}

	public function actionPhotoGroup($id)
	{
		$model = Photos::model()->findByPk($id);
		$photos = $model->album->photos;
		$all = count($photos);
		$prev = $photos[$all-1];
		$next = $photos[0];
		$crnt = '';
		foreach ($photos as $key => $value){
			$prev = $id==$value->id && isset($photos[$key-1]) ? $photos[$key-1] : $prev;
			$next = $id==$value->id && isset($photos[$key+1]) ? $photos[$key+1] : $next;
			$crnt = $id==$value->id ? $key+1 : $crnt;
		}
		$param = array(
			'group'=>$_GET['group'],
			'data'=>$model,
			'next'=>$next,
			'prev'=>$prev,
			'pos'=>$crnt.'/'.$all
		);
		$this->renderPartial('photoAjaxGroup', $param );
	}
	public function actionPhotoEvent($id)
	{
		$model = Photos::model()->findByPk($id);
		$photos = $model->album->photos;
		$all = count($photos);
		$prev = $photos[$all-1];
		$next = $photos[0];
		$crnt = '';
		foreach ($photos as $key => $value){
			$prev = $id==$value->id && isset($photos[$key-1]) ? $photos[$key-1] : $prev;
			$next = $id==$value->id && isset($photos[$key+1]) ? $photos[$key+1] : $next;
			$crnt = $id==$value->id ? $key+1 : $crnt;
		}
		$param = array(
			'event'=>$_GET['event'],
			'data'=>$model,
			'next'=>$next,
			'prev'=>$prev,
			'pos'=>$crnt.'/'.$all
		);
		$this->renderPartial('photoAjaxEvent', $param );
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Albums::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
